import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JPasswordField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class Login {

	public JFrame frmLogIn;
	private JTextField uName;
	private JPasswordField pWord;
	
	public static String daUser;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Login window = new Login();
					window.frmLogIn.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Login() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmLogIn = new JFrame();
		frmLogIn.setTitle("Log in");
		frmLogIn.setBounds(100, 100, 450, 300);
		frmLogIn.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		uName = new JTextField();
		uName.setColumns(10);
		
		JLabel lblNewLabel = new JLabel("Please Log In");
		lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 16));
		
		pWord = new JPasswordField();
		
		JLabel lblNewLabel_1 = new JLabel("Username");
		
		JLabel lblPassword = new JLabel("Password");
		
		/*
		 * 
		 * LOGIN
		 *   |
		 *   v
		 * 
		 */
		JButton log = new JButton("Login");
		log.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Database db = new Database();
				String user = uName.getText().toString();
				String pass = pWord.getText().toString();
				System.out.println(pass);
				if(db.login(user, pass)){
					frmLogIn.setVisible(false);
					daUser = user;
					MainScreen m = new MainScreen();
					m.frmMainScreen.setVisible(true);
				}else{
					System.out.println("Stahp");
				}
			}
		});
		
		/*
		 * 
		 * REGISTER
		 *    |
		 *    v
		 * 
		 */
		JButton register = new JButton("Register");
		register.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				frmLogIn.setVisible(false);
				register r = new register();
				r.frame.setVisible(true);
			}
		});
		
		
		GroupLayout groupLayout = new GroupLayout(frmLogIn.getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
							.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
								.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 125, GroupLayout.PREFERRED_SIZE)
								.addGap(151))
							.addGroup(groupLayout.createSequentialGroup()
								.addComponent(uName, GroupLayout.PREFERRED_SIZE, 138, GroupLayout.PREFERRED_SIZE)
								.addContainerGap(156, Short.MAX_VALUE))
							.addGroup(groupLayout.createSequentialGroup()
								.addComponent(pWord)
								.addGap(286))
							.addGroup(groupLayout.createSequentialGroup()
								.addComponent(lblNewLabel_1)
								.addContainerGap(378, Short.MAX_VALUE))
							.addGroup(groupLayout.createSequentialGroup()
								.addComponent(lblPassword)
								.addContainerGap(378, Short.MAX_VALUE))
							.addGroup(groupLayout.createSequentialGroup()
								.addComponent(log)
								.addContainerGap(335, Short.MAX_VALUE)))
						.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
							.addComponent(register)
							.addContainerGap())))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE)
					.addGap(12)
					.addComponent(lblNewLabel_1)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(uName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addGap(28)
					.addComponent(lblPassword)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(pWord, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addComponent(log)
					.addGap(18)
					.addComponent(register)
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		frmLogIn.getContentPane().setLayout(groupLayout);
	}
}
